#include "hal_sensor_device.h"

#include <HardwareSerial.h>
#include <M5EPD.h>
#include <utility/SHT3x.h>
#include <math.h>

HalSensorDevice::HalSensorDevice(SHT3x& dev) : _device(dev) {}

bool HalSensorDevice::Measure() {
  if (!_inited) {
    _device.Begin();
    _inited = true;
  }

  // SHT3x::UpdateData 正常返回 SHT3x_ERROR_OK
  if (_device.UpdateData() != SHT3x::SHT3x_ERROR_OK) {
    Serial.println("SHT3x measure failed!");
    return false;
  }
  return true;
}

float HalSensorDevice::GetTemperature() {
  if (!_inited) {
    Serial.println("SHT3x not inited!");
    return NAN;
  }

  float value = _device.GetTemperature();
  // Serial.printf("SHT3x get temp success: %f\n", value);
  return value;
}

float HalSensorDevice::GetHumidity() {
  if (!_inited) {
    Serial.println("SHT3x not inited!");
    return NAN;
  }

  float value = _device.GetRelHumidity();
  // Serial.printf("SHT3x get humi success: %f\n", value);
  return value;
}
